from urllib import request
import re
import csv
import random
import time


def analyzeHeros(filename):
    pattern = re.compile(r'herodetail\/\d+\.shtml')
    data = ''
    with open(filename, "r", encoding="utf8") as f:
        data = f.read()
    links = pattern.findall(data)
    return links


def claw(url):
    res = ''
    url_prefix = "https://pvp.qq.com/web201605/"
    req = request.Request(url_prefix + url)
    req.add_header(
        'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' +
        'AppleWebKit/537.36 (KHTML, like Gecko) ' +
        'Chrome/81.0.4044.92 Safari/537.36')
    with request.urlopen(req) as f:
        res = f.read().decode('gbk')

    pattern = re.compile(r'<i class="ibar" style="width:\d+%">')
    ability = pattern.findall(res)

    match = re.search(r'\d+', ability[0])
    attr0 = int(match.group(0)) // 10

    match = re.search(r'\d+', ability[1])
    attr1 = int(match.group(0)) // 10

    match = re.search(r'\d+', ability[2])
    attr2 = int(match.group(0)) // 10

    match = re.search(r'\d+', ability[3])
    attr3 = int(match.group(0)) // 10

    match = re.search(r'<h2 class="cover-name">.+</h2>', res)
    match = re.search(r'>.+<', match.group(0))
    attr4 = match.group(0)[1:-1]

    hero = {
        "名字": attr4,
        "生存能力": attr0,
        "攻击伤害": attr1,
        "技能效果": attr2,
        "上手难度": attr3
    }
    print("英雄信息增加：", hero)
    return hero


def save(objs):

    with open('heroes.csv', 'w', newline='', encoding="utf8") as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=',',
                                quotechar='|',
                                quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow(["名字", "生存能力", "攻击伤害", "技能效果", "上手难度"])
        for obj in objs:
            spamwriter.writerow([
                obj["名字"], obj["生存能力"], obj["攻击伤害"], obj["技能效果"], obj["上手难度"]
            ])


def main():
    heros = analyzeHeros("lists.html")

    ress = []
    for t in heros:
        res = claw(t)
        r = random.randint(15, 30)
        print("%d秒后抓取下一条" % r)
        time.sleep(r)
        ress.append(res)

    save(ress)


if __name__ == "__main__":
    main()
